<template>
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div class="app-container">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-card class="box-card">
            <div>
              <el-form ref="form" size="small" :model="form" :rules="rules" label-width="98px" :inline="true">
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="过磅吨位:" prop="dunwei">
                      <el-input-number v-model="form.dunwei" :precision="4" style="width: 150px;"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="到货总价:" prop="daohuoMoney">
                      <el-input-number v-model="form.daohuoMoney" :precision="2" style="width: 150px;"  disabled/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="到货时间" prop="arrivalDate">
                      <el-date-picker clearable
                                      v-model="form.arrivalDate"
                                      type="datetime"
                                      value-format="yyyy-MM-dd HH:mm:ss"
                                      placeholder="请选择到货日期">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="运费:" prop="yunfei">
                      <el-input-number v-model="form.yunfei" :precision="2" style="width: 150px;"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="送货人:" prop="songhuoid">
                      <el-select v-model="form.songhuoid" placeholder="请选择送货人" filterable @change="selectSonghuo">
                        <el-option v-for="dict in dict.type.goods_people" ref="myselectSonghuo"
                                   :key="dict.value" :label="dict.label" :value="dict.value"/>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="电话:" prop="songhuotel">
                      <el-input v-model="form.songhuotel" disabled></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="开户行:" prop="songkaihuhang">
                      <el-input v-model="form.songkaihuhang" disabled></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="账号:" prop="songzhanghao">
                      <el-input v-model="form.songzhanghao" disabled></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="24">
                    <el-form-item label="备注:" prop="remark">
                      <el-input v-model="form.remark"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="过磅单" prop="guobangdan">
                      <image-upload v-model="form.guobangdan" :limit="1"></image-upload>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </el-card>
        </el-col>
        <el-col :span="16">
          <el-card class="box-card">
            <el-table border v-loading="loading" :data="goodsSalebuyallList" ref="table" :max-height="tableMaxHeight">
              <el-table-column label="产品名称" align="center" prop="goodsName"/>
              <el-table-column label="产品型号" align="center" prop="goodModel"/>
              <el-table-column label="采购数量" align="center" prop="shengyu"/>
              <el-table-column label="采购重量" align="center" prop="dun"/>
              <el-table-column label="已到货" align="center" prop="daohuoCount"/>
              <el-table-column label="剩余到货" align="center" prop="shengyuDaohuoCount"/>
              <el-table-column label="到货状态" align="center" prop="daohuoStatus">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.daohuo_status" :value="scope.row.daohuoStatus"/>
                </template>
              </el-table-column>
              <el-table-column label="单价" width="150">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.goodsBuyPrice" :precision="2" size="small" :min="0" :disabled="scope.row.daohuoStatus == '2'"
                                   @change="calculateTotalPrice"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column label="本次到货" width="150">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.daohuoCountTemp"  :value="11" :precision="2" size="small" :min="0"
                                   :disabled="scope.row.daohuoStatus == '2'" @change="calculateTotalPrice"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="180"
                               fixed="right">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-s-operation" @click="showDaohuoDetail(scope.row)">
                    直发货明细
                  </el-button>
                  <!--<el-popconfirm :title="`确认全部到货吗？`" @confirm="goodsAllArrived(scope.row)"
                                 v-if="scope.row.daohuoStatus !== '2'"
                                 style="margin-left: 10px;">
                    <el-button slot="reference" size="mini" type="text" icon="el-icon-finished"
                               v-hasPermi="['develop:LvBuy:remove']">全部直发货
                    </el-button>
                  </el-popconfirm>-->
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize" @pagination="getList"/>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handledZhifahuo">确 定 直 发 货</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

    <lv-daohuo-detail-list ref="daohuoDetailList" @ok="getList"></lv-daohuo-detail-list>
  </el-dialog>
</template>

<script>
import {getAction, addAction, getById, accAdd,accSub,accMul, accDiv} from '@/api/manage'
import LvDaohuoDetailList from "@/views/develop/lvDaohuoDetail/module/LvDaohuoDetailList";
import {parseTime} from "@/utils/common";

export default {
  name: "ZhifahuoForm.",
  dicts: ['goods_people', 'daohuo_status'],
  components: {
    LvDaohuoDetailList,
  },
  data() {
    return {
      title: "直发货处理",
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      rows: [],
      rules: {
        arrivalDate: [
          {required: true, message: "请选择到货时间", trigger: "blur"}
        ],
        dunwei: [
          {required: true, message: "请输入吨位", trigger: "blur"}
        ],
        yunfei: [
          {required: true, message: "请输入运费", trigger: "blur"}
        ],

      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售信息管理表格数据
      goodsSalebuyallList: [],
      form: {
        dunwei: null,
        arrivalDate: null,
        guobangdan: null,
        daohuoMoney: 0.00,
        yunfei: null,
        songhuoid: null,
        songhuoren: null,
        songhuotel: null,
        songkaihuhang: null,
        songzhanghao: null,
        remark: null,
      },
      // 查询参数
      queryParams: {
        cgallid: null,
        pageNum: 1,
        pageSize: 20,
        mainId: null,
        name: null,
        brand: null,
        unit: null,
        costPrice: null,
        count: null,
        cost: null,
        validityTerm: null,
        buyId: null,
        buyName: null,
        buyDate: null,
        buyType: null,
        reimburse: null,
        supplierId: null,
        supplierName: null,
        dateRange: "",
        params: {},
      },
      url: {
        list: '/goodsSalebuyall/goodsSalebuyall/list',
        zhifahuoAdd: '/lvDaohuo/lvDaohuo/zhifahuoAdd',
        goodsPeopleById: '/goodsPeople/goodsPeople/',
        goodsAllArrived: '/goodsSalebuyall/goodsSalebuyall/goodsAllArrived',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.reset();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight);
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dunwei: null,
        arrivalDate: null,
        guobangdan: null,
        daohuoMoney: 0.00,
        yunfei: null,
        songhuoid: null,
        songhuoren: null,
        songhuotel: null,
        songkaihuhang: null,
        songzhanghao: null,
        remark: null,
      };
      this.resetForm("form");
    },
    selectSonghuo(value) {
      let that = this;
      that.$nextTick(() => {
        let arr = that.$refs.myselectSonghuo;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].currentValue == value) {
            that.form.songhuoren = arr[i].label;
            getById(this.url.goodsPeopleById, value).then(response => {
              that.form.songhuotel = response.data.songhuotel;//开户行
              that.form.songkaihuhang = response.data.songkaihuhang;//账号
              that.form.songzhanghao = response.data.songzhanghao;//行号
            });
          }
        }
      })
    },
    showDaohuoDetail(row) {
      let saleId = row.mainId;
      let goodId = row.goodsId;
      this.$refs.daohuoDetailList.showDaohuoDetailList(saleId, goodId);
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    showzhifahuo(row) {
      this.queryParams.mainId = row.id;
      this.form.buyId = row.id;
      this.form.arrivalDate = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}');
      this.open = true;
      this.getList();
    },
    handledZhifahuo() {
      let that = this;
      that.$refs["form"].validate(valid => {
        if (valid) {
          that.form.goodsSalebuyallList = that.goodsSalebuyallList;
          addAction(that.url.zhifahuoAdd, that.form).then(response => {
            that.$modal.msgSuccess("直发货成功");
            that.$emit('ok');
            that.reset();
            that.open = false;
          });
        }
      });
    },
    /** 查询销售信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.goodsSalebuyallList = response.rows;
        this.goodsSalebuyallList.forEach(item => {
          item.daohuoCountTemp = item.shengyuDaohuoCount
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    calculateTotalPrice() {
      this.form.daohuoMoney = this.goodsSalebuyallList.reduce((total, item) => {
        return total + accMul(item.goodsBuyPrice,item.daohuoCountTemp);
      }, 0);
    },

    goodsAllArrived(row) {
      let that = this;
      let params = {"id": row.id,"mainId": row.mainId};
      getAction(this.url.goodsAllArrived, params).then(response => {
        that.getList();
      });

    }


  }
};
</script>
